home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 06 - 1990 / 06.06 Jun 90 / Timing Files / Timing.a < prev    next >
Encoding:
Text File  |  1989-05-26  |  5.2 KB  |  213 lines  |  [TEXT/MPS ]

  1. *---------------------------------------------------------------------------
  2. *
  3. *            IMPLEMENTATION of UNIT Timing
  4. *
  5. *            Version    1.0    / O. Maquelin /    22-May-89
  6. *
  7. *            ***    Runs only on Macintosh II ***
  8. *
  9. *---------------------------------------------------------------------------
  10.             
  11.             CASE        ON
  12.             MACHINE        MC68020                ; needs    68020 instructions
  13.  
  14. HWNonPortable EQU        1                    ; needs    Mac    II hardware
  15. onMac        EQU            0
  16. onNuMac        EQU            1
  17.  
  18.  
  19.             INCLUDE        'HardwareEqu.a'
  20.             INCLUDE        'SysEqu.a'
  21.  
  22.  
  23.             EXPORT        (unitComp, totComp): DATA
  24.             EXPORT        (INITTIMER,    STARTTIMER,    STOPTIMER):    CODE
  25.  
  26.  
  27. ClkPerTick    EQU            13024                ; cycles per tick (16.663 ms)
  28.  
  29.  
  30. Timer        RECORD        0                    ; local definition of Timer
  31. hi            DS.L        1                    ; high longword
  32. lo            DS.L        1                    ; low longword
  33.             ENDR
  34.  
  35.  
  36. *---------------------------------------------------------------------------
  37. *
  38. *            Declaration of the exported variables
  39. *
  40. *---------------------------------------------------------------------------
  41.  
  42. unitComp    RECORD        EXPORT                ; 27 cycles compensation (35µs)
  43.             DC.L        27
  44.             ENDR
  45.  
  46. totComp        RECORD        EXPORT                ; totComp initially zero
  47. hi            DC.L        0
  48. lo            DC.L        0
  49.             ENDR
  50.  
  51.  
  52. *---------------------------------------------------------------------------
  53. *
  54. *            PROCEDURE InitTimer    (VAR t:    TimeRec)
  55. *
  56. *            Initializes a timer (t := 0)
  57. *
  58. *---------------------------------------------------------------------------
  59.  
  60. INITTIMER    PROC        EXPORT
  61.  
  62.             MOVE.L        (SP)+,A0            ; get return address
  63.             MOVE.L        (SP)+,A1            ; get address of t
  64.  
  65.             CLR.L        (A1)+                ; clear two longwords
  66.             CLR.L        (A1)
  67.  
  68.             JMP            (A0)                ; back to caller
  69.             ENDPROC
  70.  
  71.  
  72. *---------------------------------------------------------------------------
  73. *
  74. *            PROCEDURE GetTime (hi: D0.L; lo: D1.L)
  75. *
  76. *            GetTime returns the actual time in clock cycles (1.2766 µs per
  77. *            cycle) in the registers D0 and D1. Time is determined from the
  78. *            global variable Ticks and from the state of VIA 2.
  79. *
  80. *---------------------------------------------------------------------------
  81.  
  82. GetTime        PROC        ENTRY
  83.  
  84.             MOVE.L        #VBase2,A1            ; get base address of VIA2
  85.             
  86.             MOVE        SR,-(SP)            ; disable interrupts
  87.             ORI            #$0700,SR
  88.             
  89.             MOVE.B        vT1CH(A1),D1        ; read high byte of timer 1
  90.             MOVE.B        vBufB(A1),D0        ; read state of pseudo-VBL
  91.             MOVE.B        vT1C(A1),D2            ; read low byte of timer 1
  92.             ROR.W        #8,D2
  93.             MOVE.B        vT1CH(A1),D2        ; read high byte a second time
  94.             
  95.             CMP.B        D1,D2                ; if both are equal we are done,
  96.             BEQ.S        @1                    ; else read everything once more
  97.  
  98.             MOVE.B        vBufB(A1),D0        ; read state of pseudo-VBL
  99.             MOVE.B        vT1C(A1),D2            ; read low byte of timer 1
  100.             ROR.W        #8,D2
  101.             MOVE.B        vT1CH(A1),D2        ; read high byte of timer 1
  102.             
  103. @1            ROR.W        #8,D2                ; exchange low and high byte
  104.             
  105.             MOVEQ        #7,D1                ; first phase of the tick?
  106.             BTST.L        D1,D0
  107.             BNE.S        @2            
  108.             ADD.W        #ClkPerTick/2,D2    ; no, correct number of cycles
  109.  
  110. @2            MOVE.L        Ticks,D1            ; read Ticks
  111.             
  112.             MOVE        (SP)+,SR            ; enable interrupts
  113.             
  114.             CMP.W        #ClkPerTick-10,D2    ; was the value of Ticks valid?
  115.             BLE.S        @3
  116.             ADDQ        #1,D1                ; no, correct the value read
  117.             
  118. @3            MULU.L        #ClkPerTick,D0:D1    ; convert ticks to cycles and
  119.             EXT.L        D2                    ; subtract VIA value
  120.             SUB.L        D2,D1
  121.             MOVEQ        #0,D2
  122.             SUBX.L        D2,D0
  123.             
  124.             RTS
  125.             ENDPROC
  126.  
  127.  
  128. *---------------------------------------------------------------------------
  129. *
  130. *            PROCEDURE StartTimer (VAR t: Timer)
  131. *
  132. *            Starts a timer (t := t - Time + totComp;
  133. *                            totComp := totComp + unitComp)
  134. *
  135. *---------------------------------------------------------------------------
  136.  
  137. STARTTIMER    PROC        EXPORT
  138.             
  139.             MOVEC        CACR,D0                ; disable cache, save old state
  140.             MOVE.L        D0,A0
  141.             AND.B        #$FE,D0
  142.             MOVEC        D0,CACR
  143.             
  144.             MOVE.L        unitComp,D0            ; increment total compensation
  145.             ADD.L        D0,totComp.lo
  146.             BCC.S        @1                    ; is there a carry to add
  147.             ADDQ        #1,totComp.hi        ; yes, increment high word
  148.             
  149. @1            JSR            GetTime                ; determine actual time
  150.             
  151.             MOVE.L        4(SP),A1            ; get address of t
  152.             
  153.             MOVE.L        totComp.hi,D2        ; subtract compensation
  154.             SUB.L        totComp.lo,D1
  155.             SUBX.L        D2,D0
  156.             
  157.             MOVE.L        Timer.hi(A1),D2        ; subtract result from timer
  158.             SUB.L        D1,Timer.lo(A1)
  159.             SUBX.L        D0,D2
  160.             MOVE.L        D2,Timer.hi(A1)
  161.             
  162.             MOVEC        A0,CACR                ; restore cache state
  163.             
  164.             MOVE.L        (SP)+,A0            ; return to caller
  165.             ADDQ        #4,SP
  166.             JMP            (A0)
  167.             ENDPROC
  168.  
  169.  
  170. *---------------------------------------------------------------------------
  171. *
  172. *            PROCEDURE StopTimer    (VAR t:    Timer)
  173. *
  174. *            Stops a timer (t := t + Time - totComp;
  175. *                            totComp := totComp + unitComp)
  176. *
  177. *---------------------------------------------------------------------------
  178.  
  179. STOPTIMER    PROC        EXPORT
  180.  
  181.             MOVEC        CACR,D0                ; disable cache, save old state
  182.             MOVE.L        D0,A0
  183.             AND.B        #$FE,D0
  184.             MOVEC        D0,CACR
  185.             
  186.             MOVE.L        unitComp,D0            ; increment total compensation
  187.             ADD.L        D0,totComp.lo
  188.             BCC.S        @1                    ; is there a carry to add
  189.             ADDQ        #1,totComp.hi        ; yes, increment high word
  190.             
  191. @1            JSR            GetTime                ; determine actual time
  192.             
  193.             MOVE.L        4(SP),A1            ; get address of t
  194.  
  195.             MOVE.L        totComp.hi,D2        ; subtract compensation
  196.             SUB.L        totComp.lo,D1
  197.             SUBX.L        D2,D0
  198.             
  199.             MOVE.L        Timer.hi(A1),D2        ; add result to timer
  200.             ADD.L        D1,Timer.lo(A1)
  201.             ADDX.L        D0,D2
  202.             MOVE.L        D2,Timer.hi(A1)
  203.             
  204.             MOVEC        A0,CACR                ; restore cache state
  205.             
  206.             MOVE.L        (SP)+,A0            ; return to caller
  207.             ADDQ        #4,SP
  208.             JMP            (A0)
  209.             ENDPROC
  210.  
  211.  
  212.             END
  213.